package net.jiastudy.biz.project.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.jiastudy.biz.project.entity.ProjectImporter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import net.jiastudy.biz.project.entity.DeptProjectReport;
import net.jiastudy.biz.project.entity.DeptProjectReportMoneySchedule;
import net.jiastudy.biz.project.entity.DeptProjectReportWorkSchedule;
import net.jiastudy.biz.project.mapper.DeptProjectReportMapper;
import net.jiastudy.biz.project.mapper.DeptProjectReportMoneyScheduleMapper;
import net.jiastudy.biz.project.mapper.DeptProjectReportWorkScheduleMapper;
import net.jiastudy.biz.project.service.DeptProjectReportService;
import net.jiastudy.core.base.BaseService;

@Service
@Transactional(rollbackFor = Exception.class)
public class DeptProjectReportServiceImpl extends BaseService<DeptProjectReportMapper, DeptProjectReport> implements DeptProjectReportService {

	@Resource
	private DeptProjectReportWorkScheduleMapper workScheduleMapper;
	
	@Resource
	private DeptProjectReportMoneyScheduleMapper moneyScheduleMapper;
	
	@Override
	public DeptProjectReport createReport(DeptProjectReport report) {
		if (this.insert(report)) {
			if (!report.getWorkSchedules().isEmpty()) {
				for (DeptProjectReportWorkSchedule workSchedule : report.getWorkSchedules()) {
					workSchedule.setProjectId(report.getProjectId());
					workSchedule.setReportId(report.getId());
					workSchedule.setId(null);
					this.createWorkSchedule(workSchedule);
				}
			}
			if (!report.getMoneySchedules().isEmpty()) {
				for (DeptProjectReportMoneySchedule moneySchedule : report.getMoneySchedules()) {
					moneySchedule.setProjectId(report.getProjectId());
					moneySchedule.setReportId(report.getId());
					moneySchedule.setId(null);
					this.createMoneySchedule(moneySchedule);
				}
			}
			return report;
		} else {
			return null;
		}
	}
	
	public DeptProjectReportMoneySchedule createMoneySchedule(DeptProjectReportMoneySchedule moneySchedule) {
		moneyScheduleMapper.insert(moneySchedule);
		return moneySchedule;
	}
	
	public DeptProjectReportWorkSchedule createWorkSchedule(DeptProjectReportWorkSchedule workSchedule) {
		workScheduleMapper.insert(workSchedule);
		return workSchedule;
	}
	
	@Override
	public DeptProjectReport getById(Long id) {
		DeptProjectReport report = this.selectById(id);
		report.setMoneySchedules(this.findMoneyScheduleByReportId(id));
		report.setWorkSchedules(this.findWorksScheduleByReportId(id));
		return report;
	}
	
	public List<DeptProjectReportMoneySchedule> findMoneyScheduleByReportId(Long id) {
		Wrapper<DeptProjectReportMoneySchedule> wrapper = new EntityWrapper<>();
		wrapper.eq("report_id", id);
        return moneyScheduleMapper.selectList(wrapper);
	}
	
	public List<DeptProjectReportWorkSchedule> findWorksScheduleByReportId(Long id) {
		Wrapper<DeptProjectReportWorkSchedule> wrapper = new EntityWrapper<>();
		wrapper.eq("report_id", id);
        return workScheduleMapper.selectList(wrapper);
	}
	
	@Override
	public DeptProjectReport updateReport(DeptProjectReport report) {
		if (this.updateById(report)) {
			return report;
		}
		return null;
	}

	@Override
	public Boolean deleteReport(Long reportId) {
		this.deleteById(reportId);

		return true;
	}

	@Override
	public Boolean deleteReportByProjectId(Long projectId) {
		Wrapper<DeptProjectReport> wrapper = new EntityWrapper<>();
		wrapper.eq("project_id", projectId);

		this.delete(wrapper);
		return true;
	}

	@Override
	public DeptProjectReportWorkSchedule updateWorkSchedule(DeptProjectReportWorkSchedule workSchedule) {
		workScheduleMapper.updateById(workSchedule);
		return workSchedule;
	}

	@Override
	public DeptProjectReportMoneySchedule updateMoneySchedule(DeptProjectReportMoneySchedule moneySchedule) {
		moneyScheduleMapper.updateById(moneySchedule);
		return moneySchedule;
	}
	
	public List<DeptProjectReport> findReportsByProjectId(Long projectId) {
		Wrapper<DeptProjectReport> wrapper = new EntityWrapper<>();
		wrapper.eq("project_id", projectId);
        return this.selectList(wrapper);
	}

	@Override
	public DeptProjectReport getLastReport(Long projectId) {
		Wrapper<DeptProjectReport> wrapper = new EntityWrapper<>();
		wrapper.eq("project_id", projectId);
		wrapper.orderBy("report_date", false);

		DeptProjectReport lastReport = this.selectOne(wrapper);
		if (null != lastReport) {
			lastReport.setMoneySchedules(this.findMoneyScheduleByReportId(lastReport.getId()));
			lastReport.setWorkSchedules(this.findWorksScheduleByReportId(lastReport.getId()));
		}

		return lastReport;
	}
}
